package com.example.administrator.myapplication8_4;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    Button mBtnGetText;
    TextView mTvData;

    //6.2.1定义Handler
    Handler handler=new Handler(){
        public void handleMessage(Message msg){
           if(msg.what==101){
               //获取消息中的数据
               String data=(String)msg.obj;
               //将data作文文本框中的内容
               mTvData.setText(data);
           }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();

        mBtnGetText=(Button) findViewById(R.id.id_btn_get_text);
    }

    private void initEvent() {
        mBtnGetText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //创建线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //访问网络
                        try {
                            //1.构建URL对象url
                            URL url=new URL("http://restaurant.t.imooc.io/basePro/product_find");
                        //2.获取HttpURLConnection对象
                            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                        //3.设置访问方式
                            conn.setRequestMethod("GET");
                        //4.设置连接超时
                            conn.setConnectTimeout(5000);
                        //5.获取返回的数据字节流
                            InputStream is=conn.getInputStream();
                        //6.处理数据
                            //6.1 获取字符串
                            byte[] buffer = new byte[1024];
                            StringBuffer sb=new StringBuffer();
                            int len=0;
                            while ((len=is.read(buffer))!=-1){
                                String tmp=new String(buffer,0,len);
                                sb.append(tmp);
                            }
                            String data=sb.toString();
                            //6.2显示到界面
                                //6.2.2构建并发送消息
                            Message msg=new Message();
                            msg.what=101;
                            msg.obj=data;
                            handler.sendMessage(msg);

                        //7.关闭连接
                            conn.disconnect();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }

    private void initView() {mBtnGetText=(Button) findViewById(R.id.id_btn_get_text);
        mTvData=(Button)findViewById(R.id.id_btn_get_text);
    }
}
